package models

import (
	"time"
	"user_srv/basic/config"
)

// MatchRecord 匹配记录表：记录相互喜欢的用户
// 表名：match_record
type MatchRecord struct {
	Id           uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	User1Id      uint64    `gorm:"column:user1_id;type:bigint UNSIGNED;index;not null;comment:用户1ID;" json:"user1_id"`
	User2Id      uint64    `gorm:"column:user2_id;type:bigint UNSIGNED;index;not null;comment:用户2ID;" json:"user2_id"`
	MatchScore   float64   `gorm:"column:match_score;type:decimal(5,4);default:0;comment:匹配分数;" json:"match_score"`
	Status       int8      `gorm:"column:status;type:tinyint(1);default:1;comment:匹配状态 1-正常 2-解除匹配;" json:"status"`
	FirstMessage string    `gorm:"column:first_message;type:varchar(200);default:NULL;comment:首次聊天消息;" json:"first_message"`
	CreatedAt    time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt    time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt    time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// TableName 返回 MatchRecord 对应的数据库表名："match_record"
func (MatchRecord) TableName() string {
	return "match_record"
}

// GetByUsers 根据两个用户ID获取匹配记录
func (m *MatchRecord) GetByUsers() error {
	return config.DB.Where("(user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)",
		m.User1Id, m.User2Id, m.User2Id, m.User1Id).First(&m).Error
}

// Create 创建匹配记录
func (m *MatchRecord) Create() error {
	return config.DB.Create(&m).Error
}

// Update 更新匹配记录
func (m *MatchRecord) Update() error {
	return config.DB.Save(m).Error
}
